<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\SoftDeletes;



class Auth extends Authenticatable
{
    use Notifiable, HasRoles, Filterable, SoftDeletes;

    protected $table = 'admin_users';

    protected $appends = ['role_name'];

    protected $fillable = [
        'name', 'phone', 'password','type','parent_id','avatar','available','last_ip','state'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 
    ];

    public function getRoleNameAttribute()
    {
        $data = [
            1 => '平台',
            2 => '一级经销商',
            3 => '商户',
            4 => '操作员',
        ];
        if($this->parent_id && $this->type == 2){
            return '二级经销商';
        }
        return $data[$this->type];
    }

    public function manager_machine()
    {
        return $this->hasMany(MachineManager::class, 'manager_id', 'id');
    }

    public function machine()
    {
        return $this->belongsToMany(Machine::class, 'machine_managers', 'manager_id', 'machine_id');
    }

    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id', 'id');
    }

    public function getPrentAll($manager)
    {
        static $list = [];
        if(!$manager){
            return $list;
        }
        if($manager->parent_id){
            $list[] = $manager = $manager->find($manager->parent_id);
            $manager->getPrentAll($manager);
        }
        return $list;
    }

    public function third()
    {
        return $this->hasOne(ManagerThirdAuth::class, 'manager_id', 'id');
    }

}
